package org.dey.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.dey.pojo.Menu;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MenuMapper extends BaseMapper<Menu> {

    //用户登录可以访问那些菜单
    public List<Menu> querycodebyloginname(@Param("id") int id, @Param("pid") int pid);

    //修改菜单状态（删除菜单）
    @Update("update t_menus set level='0' where id=#{id}")
    public int delmenu(int id);

    @Select("SELECT tm.*\n" +
            "FROM \n" +
            "`t_user` tu INNER JOIN `t_user_role` tur \n" +
            "ON tu.`id` = tur.`user_id`\n" +
            "INNER JOIN `t_role` tr \n" +
            "ON tur.`role_id` = tr.`id`\n" +
            "INNER JOIN `t_role_menu` trm\n" +
            "ON tr.`id` = trm.`role_id`\n" +
            "INNER JOIN `t_menu` tm\n" +
            "ON trm.`menu_id` = tm.`id` WHERE tm.`level` = 1 AND tu.`id` = #{id}")
    List<Menu> queryyijicaidan(int id);

    @Select("SELECT tm.*\n" +
            "FROM \n" +
            "`t_user` tu INNER JOIN `t_user_role` tur \n" +
            "ON tu.`id` = tur.`user_id`\n" +
            "INNER JOIN `t_role` tr \n" +
            "ON tur.`role_id` = tr.`id`\n" +
            "INNER JOIN `t_role_menu` trm\n" +
            "ON tr.`id` = trm.`role_id`\n" +
            "INNER JOIN `t_menu` tm\n" +
            "ON trm.`menu_id` = tm.`id` WHERE tm.`level` = 2 AND tm.parentMenuId = #{pid} AND tu.`id` = #{userid}")
    List<Menu> queryerjicaidan(@Param("pid") Integer pid,@Param("userid") int userid);

    @Select("select * from t_menu where level = 1")
    @Results(
            id = "menusQbXxyiji" , value = {
            @Result(column = "name", property = "label"),
    }
    )
    List<Menu> menusQbXxyiji();

    @Select("select * from t_menu where parentMenuId = #{id}")
    @Results(
            id = "menusQbXxerji" , value = {
            @Result(column = "name", property = "label"),
    }
    )
    List<Menu> menusQbXxerji(int id);

    @Select("SELECT tm.*\n" +
            "FROM t_role tr INNER JOIN  t_role_menu trm ON tr.`id` = trm.`role_id` \n" +
            "INNER JOIN t_menu tm ON trm.`menu_id` = tm.`id` WHERE tm.level =2 and tr.`id` = #{rid}")
    List<Menu> menusid(int rid);


    @Insert("INSERT INTO `t_role_menu` VALUES(#{roleid},#{checkedKeys}) ")
    int shouquan(@Param("roleid") int roleid,@Param("checkedKeys") int checkedKeys);

    @Delete("delete from t_role_menu where role_id = #{roleid}")
    int scqx(int roleid);
}
